package cn.schoolwow.quickdao.module.database.dml.json.flow.common;

import cn.schoolwow.quickdao.annotation.IndexType;
import cn.schoolwow.quickdao.domain.QuickDAOConfig;
import cn.schoolwow.quickdao.domain.database.ddl.IndexField;
import cn.schoolwow.quickdao.domain.entity.Entity;
import cn.schoolwow.quickdao.module.database.dml.common.domain.ManipulationOption;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

public class SetJSONObjectEntityFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        QuickDAOConfig quickDAOConfig = flowContext.checkInstanceData(QuickDAOConfig.class);
        ManipulationOption manipulationOption = flowContext.checkInstanceData(ManipulationOption.class);
        String tableName = (String) flowContext.checkData("tableName");

        if(!manipulationOption.uniqueFieldNames.isEmpty()){
            return;
        }
        Entity entity = quickDAOConfig.getDatabaseEntityByTableName(tableName);
        if(null==entity){
            throw new IllegalArgumentException("数据库表不存在!表名:" + tableName);
        }
        for(IndexField indexField:entity.indexFieldList){
            if(indexField.indexType.equals(IndexType.UNIQUE)){
                manipulationOption.uniqueFieldNames.addAll(indexField.columns);
            }
        }
        flowContext.putTemporaryData("entity", entity);
    }

    @Override
    public String name() {
        return "设置数据库实体类信息";
    }
}
